package cn.est.controller;

import cn.est.constants.Constants;
import cn.est.dto.ClassifyDto;
import cn.est.dto.Result;
import cn.est.service.ClassifyService;
import cn.est.utils.RedisUtil;
import cn.est.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 首页显示
 */
@Api
@RestController
@RequestMapping("/api/home")
public class HomeController {

    @Autowired
    ClassifyService classifyService;

    @Autowired
    RedisUtil redisUtil;

    @ApiOperation(value = "首页商品分类",notes = "首页的商品分类树，包含分类、品牌、型号")
    @GetMapping(value = "/classifyTree",produces = "application/json")
    public Result<List<ClassifyDto>> list(){

        List<ClassifyDto> result = (List<ClassifyDto>) redisUtil.get(Constants.Home.CLASSIFYLIST);
        if(result == null){
            result =  classifyService.queryclassifytree();
            //把数据放入redis缓存中
            redisUtil.set(Constants.Home.CLASSIFYLIST,result,Constants.Home.CLASSIFYLIST_TIME);
        }
        return ResultUtils.returnDataSuccess(result);
    }
}
